﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.Model.Group;

namespace YouHP.Pages.User.Group
{
    public class Setting : GroupBasePage
    {
        public int Maxavatarsize = 12840;
        protected string logo;
        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected new void HandleCommon()
        {
            this.AddLocation("圈子设置", "/User/Group/Setting.aspx");

            if (GroupUserData.Id <= 0||(GroupUserData.Role != Constants.RoleCreator && GroupUserData.Role != Constants.RoleAdmin))
            {
                this.AddErrLine("您没有权限对该圈子进行管理！");
                return;
            }

        }

        protected new void HandlePost()
        {

            GroupInfo info = ValidateForm();
            if (info == null) return;
            bool succ = GroupBLL.Instance.UpdateGroup(info);
            if (succ)
            {
                this.AddMsgLine("圈子设置成功！");
                this.SetUrl("/User/Group/Setting.aspx?gid="+GroupData.Id);
                this.SetMetaRefresh();
                return;
            }

            this.AddErrLine("操作失败！");
        }

        protected new void HandleGet()
        {


        }

        GroupInfo ValidateForm()
        {
            GroupInfo info = GroupBLL.Instance.GetGroupInfo(GroupData.Id);

            info.GroupName = RequestTools.GetFormString("groupname").Trim();
            if (info.GroupName.Length == 0)
            {
                this.AddErrLine("圈子名称不能为空！");
                return null;
            }

            logo = Utils.SaveRequestAvatarFile(GroupData.Id, Maxavatarsize);
            if (logo != "nofile")
            {

                if (logo == "")
                {
                    AddErrLine(
                        string.Format("头像图片不合法, 系统要求必须为gif jpg png图片, 大小不得超过 {0} 字节！", Maxavatarsize));
                    return null;
                }
                else
                {
                    info.Logo = logo;
                }
            }

            info.Keyword = RequestTools.GetFormString("groupkeyword").Trim();
            info.Introduction = RequestTools.GetFormString("groupintro").Trim();
            info.BrowserPermission = RequestTools.GetFormInt("groupbrowser", Constants.BrowserOpen);
            if (info.BrowserPermission != Constants.BrowserOpen && info.BrowserPermission != Constants.BrowserLimit)
                info.BrowserPermission = Constants.BrowserOpen;
            info.JoinPermission = RequestTools.GetFormInt("groupjoin", Constants.JoinOpen);
            if (info.JoinPermission != Constants.JoinOpen && info.JoinPermission != Constants.JoinLimit)
                info.JoinPermission = Constants.JoinOpen;
            return info;
        }
    }
}